package dbms;

import java.sql.DatabaseMetaData;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;

public class DBCheckAndConfigurationFunctions {

	/*
	 * If program name is CLIENT it refers to the program itself.
	 * Parameters:
	 * - mainDir: 0
	 */
	public static boolean isDBConfigured()
	{
		if(DBCheckAndConfigurationFunctions.tableExists(DBTables.CONFIG))
			
			if(DBCheckAndConfigurationFunctions.isDBConfigParametersSet())
				return true;
		
		return false;
	}
	
	public static boolean isProgramConfigured(String programName)
	{
		try {
			String sql;
			PreparedStatement ps;
			ResultSet res;
		
			sql = "SELECT * FROM " + DBTables.CONFIG + " WHERE program=? AND parameter=" + DBParameters.PROGRAMDIR;
			ps = DBConnection.getIstance().prepareStatement(sql);
			ps.setString(1, programName);	
			res = ps.executeQuery();
			if(!res.next()) return false;
			
			sql = "SELECT * FROM " + DBTables.CONFIG + " WHERE program=? AND parameter=" + DBParameters.PROGRAMEXECUTINGNAME;
			ps = DBConnection.getIstance().prepareStatement(sql);
			ps.setString(1, programName);	
			res = ps.executeQuery();
			if(!res.next()) return false;
			
			/*
			sql = "SELECT * FROM " + DBTables.CONFIG + " WHERE program=? AND parameter=" + DBParameters.PROGRAMEXTENZION;
			ps = DBConnection.getIstance().prepareStatement(sql);
			ps.setString(1, programName);	
			res = ps.executeQuery();
			if(!res.next()) return false;
			*/
			
			sql = "SELECT * FROM " + DBTables.CONFIG + " WHERE program=? AND parameter=" + DBParameters.PROGRAMTYPE;
			ps = DBConnection.getIstance().prepareStatement(sql);
			ps.setString(1, programName);	
			res = ps.executeQuery();
			if(!res.next()) return false;
			
			return true;
			
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			return false;
		}
	}
	
	public static boolean isDBConfigParametersSet()
	{
		try {
			
			ResultSet res;
			Statement st = DBConnection.getIstance().createStatement();
			
			res = st.executeQuery("SELECT * FROM " + DBTables.CONFIG + " WHERE program='CLIENT' AND parameter=" + DBParameters.MAINDIR);
			if(!res.next()) return false;
			
			return true;
			
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			return false;
		}		
	}

	
	public static boolean setClientMainDir(String mainDir)
	{
		try {
			DBConnection.getIstance().setAutoCommit(false);

			String sql = "INSERT INTO " + DBTables.CONFIG + "(program, parameter, value) VALUES (?, ?, ?)";
			PreparedStatement ps = DBConnection.getIstance().prepareStatement(sql);
			ps.setString(1, "CLIENT");
			ps.setInt(2, DBParameters.MAINDIR);
			ps.setString(3, mainDir);
			ps.executeUpdate();
			
			DBConnection.getIstance().commit();
			DBConnection.getIstance().setAutoCommit(true);
			
			System.out.println("Client Main Dir Set: " + mainDir);
			
			return true;
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			return false;
		}
	}
	
	public static boolean tableExists(String tableName)
	{	
		try {
			DatabaseMetaData dbm = DBConnection.getIstance().getMetaData();
			ResultSet rs = dbm.getTables(null, null, tableName.toUpperCase(), null);
			
			return rs.next();			
		
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			return false;
		}
	}
	
}
